package com.zenithmind.library.controller;

import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.library.pojo.dto.BookReviewCreateDTO;
import com.zenithmind.library.pojo.dto.BookReviewUpdateDTO;
import com.zenithmind.library.pojo.query.BookReviewQuery;
import com.zenithmind.library.pojo.vo.BookReviewVO;
import com.zenithmind.library.service.BookReviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书评价管理控制器
 * 遵循单一职责原则：只负责图书评价相关的HTTP请求处理
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Slf4j
@RestController
@RequestMapping("/api/library/book-reviews")
@RequiredArgsConstructor
@Tag(name = "图书评价管理", description = "图书评价的增删改查、点赞、举报、审核等功能")
public class BookReviewController {

    private final BookReviewService bookReviewService;

    @GetMapping("/page")
    @Operation(summary = "分页查询图书评价", description = "根据条件分页查询图书评价列表")
    @PreAuthorize("hasAuthority('library:book-review:query')")
    public Result<PageResult<BookReviewVO>> getBookReviewPage(@Valid BookReviewQuery query) {
        log.info("分页查询图书评价，查询条件：{}", query);
        PageResult<BookReviewVO> result = bookReviewService.getBookReviewPage(query);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询图书评价详情", description = "根据ID查询图书评价详细信息")
    @PreAuthorize("hasAuthority('library:book-review:query')")
    public Result<BookReviewVO> getBookReviewById(
            @Parameter(description = "评价ID", required = true)
            @PathVariable String id) {
        log.info("查询图书评价详情，ID：{}", id);
        return bookReviewService.getBookReviewById(id);
    }

    @GetMapping("/book/{bookId}")
    @Operation(summary = "查询图书的评价列表", description = "根据图书ID查询该图书的评价列表")
    @PreAuthorize("hasAuthority('library:book-review:query')")
    public Result<List<BookReviewVO>> getBookReviewsByBookId(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId,
            @Parameter(description = "限制数量")
            @RequestParam(required = false) Integer limit) {
        log.info("查询图书的评价列表，图书ID：{}，限制数量：{}", bookId, limit);
        return bookReviewService.getBookReviewsByBookId(bookId, limit);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "查询用户的评价列表", description = "根据用户ID查询该用户的评价列表")
    @PreAuthorize("hasAuthority('library:book-review:query')")
    public Result<List<BookReviewVO>> getBookReviewsByUserId(
            @Parameter(description = "用户ID", required = true)
            @PathVariable String userId,
            @Parameter(description = "限制数量")
            @RequestParam(required = false) Integer limit) {
        log.info("查询用户的评价列表，用户ID：{}，限制数量：{}", userId, limit);
        return bookReviewService.getBookReviewsByUserId(userId, limit);
    }

    @PostMapping
    @Operation(summary = "创建图书评价", description = "创建新的图书评价")
    @PreAuthorize("hasAuthority('library:book-review:create')")
    public Result<BookReviewVO> createBookReview(@Valid @RequestBody BookReviewCreateDTO createDTO) {
        log.info("创建图书评价，参数：{}", createDTO);
        return bookReviewService.createBookReview(createDTO);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新图书评价", description = "更新图书评价信息")
    @PreAuthorize("hasAuthority('library:book-review:update')")
    public Result<BookReviewVO> updateBookReview(
            @Parameter(description = "评价ID", required = true)
            @PathVariable String id,
            @Valid @RequestBody BookReviewUpdateDTO updateDTO) {
        log.info("更新图书评价，ID：{}，参数：{}", id, updateDTO);
        return bookReviewService.updateBookReview(id, updateDTO);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除图书评价", description = "删除指定的图书评价")
    @PreAuthorize("hasAuthority('library:book-review:delete')")
    public Result<Void> deleteBookReview(
            @Parameter(description = "评价ID", required = true)
            @PathVariable String id) {
        log.info("删除图书评价，ID：{}", id);
        return bookReviewService.deleteBookReview(id);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除图书评价", description = "批量删除多个图书评价")
    @PreAuthorize("hasAuthority('library:book-review:delete')")
    public Result<Void> batchDeleteBookReviews(
            @Parameter(description = "评价ID列表", required = true)
            @RequestBody List<String> ids) {
        log.info("批量删除图书评价，数量：{}", ids.size());
        return bookReviewService.batchDeleteBookReviews(ids);
    }

    @PostMapping("/{id}/like")
    @Operation(summary = "点赞评价", description = "为指定评价点赞")
    @PreAuthorize("hasAuthority('library:book-review:like')")
    public Result<Void> likeReview(
            @Parameter(description = "评价ID", required = true)
            @PathVariable String id,
            @Parameter(description = "用户ID", required = true)
            @RequestParam String userId) {
        log.info("点赞评价，评价ID：{}，用户ID：{}", id, userId);
        return bookReviewService.likeReview(id, userId);
    }

    @DeleteMapping("/{id}/like")
    @Operation(summary = "取消点赞评价", description = "取消对指定评价的点赞")
    @PreAuthorize("hasAuthority('library:book-review:like')")
    public Result<Void> unlikeReview(
            @Parameter(description = "评价ID", required = true)
            @PathVariable String id,
            @Parameter(description = "用户ID", required = true)
            @RequestParam String userId) {
        log.info("取消点赞评价，评价ID：{}，用户ID：{}", id, userId);
        return bookReviewService.unlikeReview(id, userId);
    }

    @PostMapping("/{id}/report")
    @Operation(summary = "举报评价", description = "举报不当评价")
    @PreAuthorize("hasAuthority('library:book-review:report')")
    public Result<Void> reportReview(
            @Parameter(description = "评价ID", required = true)
            @PathVariable String id,
            @Parameter(description = "举报用户ID", required = true)
            @RequestParam String userId,
            @Parameter(description = "举报原因", required = true)
            @RequestParam String reason) {
        log.info("举报评价，评价ID：{}，用户ID：{}，原因：{}", id, userId, reason);
        return bookReviewService.reportReview(id, userId, reason);
    }

    @PutMapping("/{id}/audit")
    @Operation(summary = "审核评价", description = "审核图书评价")
    @PreAuthorize("hasAuthority('library:book-review:audit')")
    public Result<Void> auditReview(
            @Parameter(description = "评价ID", required = true)
            @PathVariable String id,
            @Parameter(description = "审核状态", required = true)
            @RequestParam Integer status,
            @Parameter(description = "审核原因")
            @RequestParam(required = false) String auditReason) {
        log.info("审核评价，评价ID：{}，状态：{}，原因：{}", id, status, auditReason);
        return bookReviewService.auditReview(id, status, auditReason);
    }

    @GetMapping("/statistics/{bookId}")
    @Operation(summary = "获取图书评价统计", description = "获取指定图书的评价统计信息")
    @PreAuthorize("hasAuthority('library:book-review:query')")
    public Result<BookReviewService.BookReviewStatisticsVO> getBookReviewStatistics(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId) {
        log.info("获取图书评价统计，图书ID：{}", bookId);
        return bookReviewService.getBookReviewStatistics(bookId);
    }

    @GetMapping("/check-reviewed")
    @Operation(summary = "检查用户是否已评价图书", description = "检查指定用户是否已评价指定图书")
    @PreAuthorize("hasAuthority('library:book-review:query')")
    public Result<Boolean> checkUserReviewedBook(
            @Parameter(description = "图书ID", required = true)
            @RequestParam String bookId,
            @Parameter(description = "用户ID", required = true)
            @RequestParam String userId) {
        log.info("检查用户是否已评价图书，图书ID：{}，用户ID：{}", bookId, userId);
        boolean hasReviewed = bookReviewService.hasUserReviewedBook(bookId, userId);
        return Result.success(hasReviewed);
    }

    @PostMapping("/import")
    @Operation(summary = "导入图书评价", description = "批量导入图书评价数据")
    @PreAuthorize("hasAuthority('library:book-review:import')")
    public Result<String> importBookReviews(
            @Parameter(description = "图书评价数据列表", required = true)
            @RequestBody List<BookReviewCreateDTO> reviews) {
        log.info("导入图书评价，数量：{}", reviews.size());
        return bookReviewService.importBookReviews(reviews);
    }

    @PostMapping("/export")
    @Operation(summary = "导出图书评价", description = "根据条件导出图书评价数据")
    @PreAuthorize("hasAuthority('library:book-review:export')")
    public Result<String> exportBookReviews(@RequestBody BookReviewQuery query) {
        log.info("导出图书评价，查询条件：{}", query);
        return bookReviewService.exportBookReviews(query);
    }
}
